<?php
$isReloadable = true;
error_reporting(E_ALL);
require_once("../../../pers/inc.config.php");
require_once OVF_FS_WEB_ROOT.'/app/_inc/inc.session.php';
require_once OVF_FS_WEB_ROOT.'/app/_inc/funct.html.php';
require_once OVF_FS_WEB_ROOT.'/app/_inc/funct.getFiles.php';
reqDef("fonte","Anag");
require OVF_FS_WEB_ROOT."/app/rcom/rice/_inc/funct.ricerca".reqVar("fonte").".php";

$sHtmlEsito = '';
if (reqVar('azione')=='ricerca') {
   $msgPag .= eseguiRicerca($sHtmlEsito);
}

$cmdL = "";

$cmdR1 = "<a href=\"#\" class=\"cmd1\">recupera parametri memorizzati</a>";
$cmdR1 .= cmdTorna(); 

$cmdR2 = "";
$cmdR2 .= "&nbsp;&nbsp;&nbsp;<a href=\"javascript:document.frm1.submit();\" class=\"cmd1\">aggiungi riga condizione</a>";
$cmdR2 .= "&nbsp;&nbsp;&nbsp;<a href=\"javascript:document.frm1.azione.value='ricerca';document.frm1.submit();\" class=\"cmd1\">esegui ricerca</a>";
$cmdR2 .= cmdTorna(); 

testaHtml('Anagrafe','Ricerca avanzata', $msgPag, 'rcom');
echo getHtmlForm();
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('azione','...');
echo getHtmlHidden('startoffset');
?>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <?=getHtmlRigaRif()?>
   <tr>
      <td class="cmd1" align="left" colspan='10'>
         <?=$cmdL?>
      </td>
      <td class="cmd1" align="right" colspan='10'>
         <?=$cmdR1?>
      </td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='2'>
         Fonte dati
      </td>
      <td class="txt1" align="left" colspan='6'>
         <?
         $aFonti = array("Anag"=>"Anagrafe");
         echo getHtmlSelect('fonte', $aFonti);
         ?>
      </td>
      <td class="lab1" align="left" colspan='4'>
         Parametri memorizzati
      </td>
      <td class="txt1" align="left" colspan='8'>
         <?
         $aParamSet = array(""=>"---");
         echo getHtmlSelect('parSet', $aParamSet);
         ?>
      </td>
   </tr>
   <tr>
      <td class="tit1" align="center" colspan='20'>
         Condizioni filtro
      </td>
   </tr>
   <tr>
      <td class="lab1" align="center" colspan="1">
         E/O
      </td>
      <td class="lab1" align="center" colspan="1">
         p.a.
      </td>
      <td class="lab1" align="center" colspan="6">
         campo
      </td>
      <td class="lab1" align="center" colspan="3">
         tipo conf.
      </td>
      <td class="lab1" align="center" colspan="8">
         valore
      </td>
      <td class="lab1" align="left" colspan="1">
         p.c.        
      </td>
   </tr>
<?
$aNN = array();
$ultimoCampo = '';
$nMax = 0;
foreach($_REQUEST as $nX => $vX) {
   if (substr($nX,0,11)!='campo_cond_') continue;
   $nn = intval(substr($nX,11));
   $aNN[] = $nn;
   $ultimoCampo = $vX;
   if ($nn > $nMax) $nMax = $nn;
}
if (count($aNN)==0 || $ultimoCampo!='') {
   $aNN[] = $nMax+1;
}
$bFirst = true;
foreach($aNN as $kk => $nn) {
?>
   <tr>
      <td class="txt1" align="center" colspan="1">
         <?
         if (!$bFirst) {
            $aAndOr = array("AND"=>"E","OR"=>"O");
            reqDef("andOr_$nn","E");
            echo getHtmlSelect("andOr_$nn", $aAndOr); 
         }
         $bFirst = false;
         ?>
      </td>
      <td class="txt1" align="center" colspan="1">
         <?
         $aParentAp = array(""=>"","("=>"(","(("=>"((","((("=>"(((");
         reqDef("parentAp_$nn","");
         echo getHtmlSelect("parentAp_$nn", $aParentAp);
         ?>
      </td>
      <td class="txt1" align="center" colspan="6">
         <?
         reqDef("campo_cond_$nn","");
         echo getHtmlSelect("campo_cond_$nn", getCampiCond());
         ?>
      </td>
      <td class="txt1" align="center" colspan="3">
         <?
         $aTipiConfr = array("like"=>"contiene","="=>"=",">"=>">","<"=>"<","<="=>"<=",">="=>">=","<>"=>"<>","vuoto"=>"vuoto","non vuoto"=>"non-vuoto");
         reqDef("tipoConfr_$nn","contiene");
         echo getHtmlSelect("tipoConfr_$nn", $aTipiConfr);
         ?>
      </td>
      <td class="txt1" align="center" colspan="8">
         <?
         reqDef("valConfr_$nn","");
         echo getHtmltext("valConfr_$nn", 30);
         ?>
      </td>
      <td class="txt1" align="left" colspan="1">
         <?
         $aParentCh = array(""=>"",")"=>")","))"=>"))",")))"=>")))");
         reqDef("parentCh_$nn","");
         echo getHtmlSelect("parentCh_$nn", $aParentCh);
         ?>        
      </td>
   </tr>
<?
}
?>
   <tr>
      <td class="cmd1" align="left" colspan='10'>
         <?=$cmdL?>
      </td>
      <td class="cmd1" align="right" colspan='10'>
         <?=$cmdR2?>
      </td>
   </tr>
<?
   echo $sHtmlEsito;
?>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function eseguiRicerca(&$sHtmlEsito) {
   global $objDB,$objSess;
   $objSess->pushHistory(false); // memorizza var post
   $sMess = '';
   $sWhere = ' WHERE ';
   $nParAp = 0;
   foreach($_REQUEST as $nX => $vX) {
      if (substr($nX,0,11)!='campo_cond_') continue;
      if ($vX=='') continue;
      $nn = substr($nX,11);
      if (reqVar("parentAp_$nn") == '(') $nParAp += 1;
      if (reqVar("parentAp_$nn") == '((') $nParAp += 2;
      if (reqVar("parentAp_$nn") == '(((') $nParAp += 3;
      if (reqVar("parentCh_$nn") == ')') $nParAp -= 1;
      if (reqVar("parentCh_$nn") == '))') $nParAp -= 2;
      if (reqVar("parentCh_$nn") == ')))') $nParAp -= 3;
      if ($nParAp < 0) {
         return "troppe parentesi chiuse";
      }
      $sWhere .= reqVar("andOr_$nn")." ".reqVar("parentAp_$nn").reqVar("campo_cond_$nn");
      switch (reqVar("tipoConfr_$nn")) {
         case 'like':
            $sWhere .= " like '%".addslashes(reqVar("valConfr_$nn"))."%' ";
            break;
         case 'vuoto':
            $sWhere .= " IN ('','0','0000-00-00') ";
            break;
         case 'non vuoto':
            $sWhere .= " NOT IN ('','0','0000-00-00') ";
            break;
         default:
            $sWhere .= reqVar("tipoConfr_$nn")." '".addslashes(reqVar("valConfr_$nn"))."'";
            break;
      }
      $sWhere .= reqVar("parentCh_$nn")." ";
      
   }   
   if ($nParAp != 0) {
      return "troppe parentesi aperte";
   }
   //return $sWhere;   
   if ($sWhere == ' WHERE ') $sWhere = '';
   $rc = getEsitoRic($sWhere, $sHtmlEsito, $sMess);
   if (!$rc) {
      $sHtmlEsito = '';
      return $sMess;
   }
   return "ricerca eseguita";
}
